local zili = fk.CreateSkill {
  name = "hx__zili",
  tags = { Skill.Limited },
}


local spec = {
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:isWounded() then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = zili.name,
      }
      if player.dead then return end
    end
    player:drawCards(2, zili.name)
    if player.dead then return end
    if player:hasSkill("hx__paiyi", true) then
      room:changeMaxHp(player, 1)
    else
      room:changeMaxHp(player, -1)
      if player.dead then return end
      room:handleAddLoseSkills(player, "hx__paiyi")
    end
  end,
}

zili:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zili.name) and player.phase == Player.Play
    and #player:getPile("hxzhaohua__zhonghui_power") > 2 and player:usedSkillTimes(zili.name, Player.HistoryGame) == 0
  end,
  on_use = spec.on_use,
})

zili:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(zili.name) and #player:getPile("hxzhaohua__zhonghui_power") > 2 and player:usedSkillTimes(zili.name, Player.HistoryGame) == 0 then
      for _, move in ipairs(data) do
        if move.from == player and move.to and move.to ~= player and move.moveReason == fk.ReasonPrey then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = spec.on_use,
})

zili:addEffect(fk.Damaged, {
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(zili.name)
    and #player:getPile("hxzhaohua__zhonghui_power") > 2 and player:usedSkillTimes(zili.name, Player.HistoryGame) == 0
  end,
  on_use = spec.on_use,
})


return zili
